package com.atguigu.datastream.day06;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;


/**
 * ClassName: Flink04_ProcessFun_WordCount
 * Package: com.atguigu.day06
 * Description:
 *               processfun实现wc
 * @Author ChenJun
 * @Create 2023/4/12 18:05
 * @Version 1.0
 */
public class Flink04_ProcessFun_WordCount {
    public static void main(String[] args) throws Exception {

        //1. 创建流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2. 从文件读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost",9999);

        //3. 将读取过来的数据切成一个个的单词
        SingleOutputStreamOperator<String> wordDStream = streamSource.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String value, ProcessFunction<String, String>.Context ctx, Collector<String> out) throws Exception {
                String[] splits = value.split(" ");
                for (String split : splits) {
                    out.collect(split);
                }

            }
        });

        //4. 将单词转换为tuple二元组
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOne = wordDStream.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void processElement(String value, ProcessFunction<String, Tuple2<String, Integer>>.Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                out.collect(Tuple2.of(value, 1));
            }
        });

        //5. 将相同的key聚合到一起
        wordToOne.process(new ProcessFunction<Tuple2<String, Integer>, Tuple2<String,Integer>>() {

            //创建 k  v 结构的集合
            HashMap<String, Integer> count = new HashMap<>();

            @Override
            public void processElement(Tuple2<String, Integer> value, ProcessFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>.Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                //判断累加器中是否有当前单词的累加器
                if(count.containsKey(value.f0)){
                    //在累加器中有当前单词的累加器
                    //取出之前累加的值
                    Integer integer = count.get(value.f0);
                    integer+=1;
                    count.put(value.f0,integer);
                }else{
                    //在累加器中没有当前单词的累加器
                    count.put(value.f0, 1);
                }
                out.collect(Tuple2.of(value.f0, count.get(value.f0)));
            }
        }).print();

        env.execute();

    }
}
